﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace canvasTest
{
    public partial class BlockDesign : UserControl
    {
        #region 依赖属性
        public static readonly DependencyProperty FillProperty =
            DependencyProperty.Register(
                nameof(Fill),
                typeof(Brush),
                typeof(BlockDesign),
                new PropertyMetadata(Brushes.Transparent));

        public static readonly DependencyProperty StrokeProperty =
            DependencyProperty.Register(
                nameof(Stroke),
                typeof(Brush),
                typeof(BlockDesign),
                new PropertyMetadata(Brushes.Black));

        public static readonly DependencyProperty StrokeThicknessProperty =
            DependencyProperty.Register(
                nameof(StrokeThickness),
                typeof(double),
                typeof(BlockDesign),
                new PropertyMetadata(1.0));
        #endregion

        #region 连接点事件
        public event MouseButtonEventHandler ConnectorClicked;

        private void Connector_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (sender == InputConnector)
                OnConnectorClicked(e, false);
            else if (sender == OutputConnector)
                OnConnectorClicked(e, true);

            e.Handled = true;
        }

        protected virtual void OnConnectorClicked(MouseButtonEventArgs e, bool isOutput)
        {
            ConnectorClicked?.Invoke(this, new BlockConnectorEventArgs(e, isOutput));
        }
        #endregion

        #region 连接点位置属性
        public Point InputConnectorPosition
        {
            get
            {
                var transform = InputConnector.TransformToVisual(Application.Current.MainWindow);
                return transform.Transform(new Point(4, 4));
            }
        }

        public Point OutputConnectorPosition
        {
            get
            {
                var transform = OutputConnector.TransformToVisual(Application.Current.MainWindow);
                return transform.Transform(new Point(4, 4));
            }
        }
        #endregion

        #region 属性访问器
        public Brush Fill
        {
            get => (Brush)GetValue(FillProperty);
            set => SetValue(FillProperty, value);
        }

        public Brush Stroke
        {
            get => (Brush)GetValue(StrokeProperty);
            set => SetValue(StrokeProperty, value);
        }

        public double StrokeThickness
        {
            get => (double)GetValue(StrokeThicknessProperty);
            set => SetValue(StrokeThicknessProperty, value);
        }
        #endregion

        public BlockDesign()
        {
            InitializeComponent();
        }
    }

    #region 自定义事件参数
    public class BlockConnectorEventArgs : MouseButtonEventArgs
    {
        public bool IsOutput { get; }

        public BlockConnectorEventArgs(MouseButtonEventArgs baseArgs, bool isOutput)
            : base(baseArgs.MouseDevice, baseArgs.Timestamp, baseArgs.ChangedButton)
        {
            IsOutput = isOutput;
            RoutedEvent = baseArgs.RoutedEvent;
            Source = baseArgs.Source;
        }
    }
    #endregion
}